package com.ncut.interceptors;

import com.ncut.context.BaseContext;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @FileName EmpInfoInterceptor
 * @Description
 * @Author Sword
 * @date 2025-08-08
 **/
public class EmpInfoInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.获取登录员工信息
        String empInfo = request.getHeader("emp-info");

        //2.判断是否获取了员工，如果有，存入ThreadLocal中
        if (empInfo != null) {
            BaseContext.setCurrentId(Long.valueOf(empInfo));
        }

        //3.放行
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        BaseContext.removeCurrentId();
    }


}
